﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using Library;

namespace CarService
{
    public partial class Contact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Load_Data();
            if (!Page.IsPostBack)
            {
            }
        }

        private void Load_Data()
        {
            SettingInfo settingInfo = SettingData.SelectBySiteID(Config.Global.Settings.SiteID);
            Page.Title = "Liên hệ - " + settingInfo.SiteName + " - " + settingInfo.SiteURL;
            Page.MetaDescription = settingInfo.SiteDescription;
            Page.MetaKeywords = settingInfo.SiteKeyword;

            lblSiteName.Text = settingInfo.SiteName;
            lblSiteAddress.Text = settingInfo.SiteAddress;
            lblPhone.Text = settingInfo.SitePhone;
            lblFax.Text = "";
            lblEmail.Text = "<a href=\"mailto:" + settingInfo.SiteEmail + "\">" + settingInfo.SiteEmail + "</a>";
            lblWebsite.Text="<a href=\"http://" + settingInfo.SiteURL +"\">" + settingInfo.SiteURL + "</a>";
        }

        private void Reset()
        {
            txtName.Text = "";
            txtEmail.Text = "";
            txtPhone.Text = "";
            txtDesc.Text = "";
        }

        protected void lbtSubmit_Click(object sender, EventArgs e)
        {
            string mailTo = txtEmail.Text.Trim();
            string mailSubject = txtName.Text.Trim() + " Gửi ý kiến góp ý.";
            string mailBody = string.Empty;
            mailBody += "Họ tên: <b>" + txtName.Text.Trim() + "</b>.<br />";
            mailBody += "Số điện thoại: <b>" + txtPhone.Text.Trim() + "</b>.<br />";
            mailBody += "Nội dung: <p>";
            mailBody += txtDesc.Text.Trim() + "</b>";

            Send(mailTo, mailSubject, mailBody);
        }

        protected void lblReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        public static void Send(string mailTo, string mailSubject, string mailBody)
        {
            string SMTPPassKey = Config.Global.Settings.SMTPPassKey;
            int SMTPPassIndex = Config.Global.Settings.SMTPPassIndex;

            SettingInfo setting = SettingData.SelectBySiteID(Config.Global.Settings.SiteID);
            EmailSettingInfo emailInfo = EmailSettingData.SelectByEmailID(Config.Global.Settings.EmailID);
            string messSMTPPass = emailInfo.SMTPPassword;
            string cipherSMTPPass = Library.StringUtility.MessString(messSMTPPass, SMTPPassIndex);
            string clearSMTPPass = Encrypt.SimpleEncryption.Decrypt(cipherSMTPPass, SMTPPassKey);

            MyMail myMail = new MyMail();

            myMail.MailFrom = emailInfo.EmailFrom;
            myMail.MailDisplay = setting.SiteName;
            myMail.MailTo = mailTo;
            myMail.MailSubject = mailSubject;
            myMail.MailBody = mailBody;
            myMail.SMTPUsername = emailInfo.SMTPUserName;
            myMail.SMTPPassword = clearSMTPPass;
            myMail.SMTPServer = emailInfo.SMTPServer;
            myMail.SMTPPort = emailInfo.SMTPPort;
            myMail.SMTPSSL = emailInfo.SMTPSSL;

            myMail.Send();
        }
    }
}